﻿// Inner Fire 游戏引擎库
// BMFONT类
//
// Copyright (c) 2024 by 尔西大帝. All rights reserved.
//
// 这个类用于载入BMFONT的文件信息
// 所属层级：
//
// Author: 尔西大帝
// Email: 2670613871@qq.com
// Created: 2024-12-19

#pragma once

#include <dx/core/dx_core.h>
#include <xml_reader.h>

using namespace ikit;

namespace ifire::ui {

struct BMFontChar {
  int id;
  int x;
  int y;
  int width;
  int height;
  int xoffset;
  int yoffset;
  int xadvance;
  int page;
  int chnl;

  XMFLOAT2 uv_point;
  XMFLOAT2 uv_size;

  BMFontChar();
  BMFontChar(const XmlNode& node, int index);
};

struct BMFontPage {
  int id;
  std::string file;
  BMFontPage() = default;
  BMFontPage(const XmlNode& node, int index);
};

// <info face="Consolas" size="-32" bold="0" italic="0" charset="" unicode="1"
// stretchH="100" smooth="1" aa="1" padding="0,0,0,0" spacing="1,1"
// outline="0"/>
struct BMFontInfo {
  std::string face;
  int size;
  int bold;
  int italic;
  std::string charset;
  int unicode;
  int stretchH;
  int smooth;
  int aa;
  Rect padding;
  Size spacing;
  int outline;
  BMFontInfo() = default;
  BMFontInfo(const XmlNode& node);
};

// <common lineHeight="37" base="29" scaleW="256" scaleH="256" pages="1"
// packed="0" alphaChnl="1" redChnl="0" greenChnl="0" blueChnl="0"/>
struct BMFontCommon {
  int lineHeight;
  int base;
  int scaleW;
  int scaleH;
  int pages;
  int packed;
  int alphaChnl;
  int redChnl;
  int greenChnl;
  int blueChnl;

  BMFontCommon() = default;
  BMFontCommon(const XmlNode& node);
};

class BMFont {
public:
  BMFont() = default;
  BMFont(const std::string_view& filename);
  BMFont(const BMFont& rhs) = delete;
  BMFont& operator=(const BMFont& rhs) = delete;
  ~BMFont() = default;

  void Load(const std::string_view& filename);
  const BMFontChar& GetChar(int id) const;

public:
  BMFontInfo mInfo;
  BMFontCommon mCommon;
  eastl::vector<BMFontPage> mPages;
  eastl::vector<BMFontChar> mChars;

  // 贴图的大小
  SizeF mSize;

private:
};

using BMFontPtr = std::unique_ptr<BMFont>;

} // namespace ifire